const input = document.getElementById('input');

//声明worker1
const worker1 = new Worker('src/one.js');
//监听来自one.js的消息
worker1.onmessage = (message) => {
    let data = message.data;
    callback('one', data);
    //终止worker1线程运行
    worker1.terminate();
};
//监听worker1的错误
worker1.onerror = (error) => {
    console.log('错误：' + error)
};

//声明worker2
const worker2 = new Worker('src/two.js');
//监听来自two.js的消息
worker2.onmessage = (message) => {
    let data = message.data;
    callback('two', data);
    //终止worker2线程运行
    worker2.terminate();
};
//监听worker2的错误
worker2.onerror = (error) => {
    console.log('错误：' + error)
};

input.addEventListener('change', function (e) {
    //向one.js发送消息
    worker1.postMessage({name: e.target.value + '1'});
    //向two.js发送消息
    worker2.postMessage({name: e.target.value + '2'});
});

function callback(type, data) {
    console.log('index.js接收到来自' + type + '.js的最新消息：');
    console.table(data);
}